
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class FileRegex {

	private String filePath;	
	private String REGEX = "\\d+([\\.\\,]\\d+)?"; // regular expression for decimal number
	private String regexForRep = ",";
	private String replacement = ".";
	
	public void setRegexForRep(String regexForRep) {
		this.regexForRep = regexForRep;
	}

	public void setReplacement(String replacement) {
		this.replacement = replacement;
	}
	
	/**
	 * A compiled representation of a regular expression. A regular expression,
	 * specified as a string, must first be compiled into an instance of this
	 * class. The resulting pattern can then be used to create a Matcher object
	 * that can match arbitrary character sequences against the regular
	 * expression.
	 */
	private Pattern p = Pattern.compile(REGEX);
	/**
	 * An engine that performs match operations on a character sequence by
	 * interpreting a Pattern.
	 */
	private Matcher m;

	// constructor
	public FileRegex(String path) {
		super();
		this.filePath = path;
	}

	// set path to file with text
	public void setFilePath(String fp) {
		this.filePath = fp;
	}
	
	// compiles the given regular expression into a pattern
	public void setPattern(String regex) {
		this.p = Pattern.compile(regex);
	}

	// we use Scanner to read line from file and find the matches with regular expression
	public ArrayList<String> getMatchesFromFile() {
		ArrayList<String> result = new ArrayList<String>();
		File file = new File(this.filePath);
		try {
			Scanner scanner = new Scanner(file);
			while (scanner.hasNextLine()) {
				String line = scanner.nextLine();				
				this.m = this.p.matcher(line.replaceAll(this.regexForRep, this.replacement));
				while (this.m.find()) {
					result.add(m.group());
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return result;
	}

}
